package org.opens.magicByte.convert;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.misterchangray.core.clazz.MResult;
import com.github.misterchangray.core.intf.MConverter;
import io.netty.buffer.ByteBufUtil;
import org.opens.util.ByteUtil;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 功能描述
 *
 * @author: zhangyuming
 * @date: 2023/9/18 15:40
 */
public class DateConvert implements MConverter<Date> {

    @Override
    public MResult<Date> pack(int nextReadIndex, byte[] fullBytes, String attachParams) {
        String dateHexString = ByteBufUtil.hexDump(fullBytes, nextReadIndex, 6);

        String dateBs = ByteUtil.hexStringToBinaryStringFixedLength(dateHexString);

        int year = Integer.parseInt(dateBs.substring(0, 12), 2);
        int month = Integer.parseInt(dateBs.substring(12, 16), 2);
        int dayOfMonth = Integer.parseInt(dateBs.substring(16, 21), 2);
        int hour = Integer.parseInt(dateBs.substring(21, 26), 2);
        int minute = Integer.parseInt(dateBs.substring(26, 32), 2);
        int second = Integer.parseInt(dateBs.substring(32, 38), 2);
        int millisecond = Integer.parseInt(dateBs.substring(38), 2);

        System.out.println("截取的月: " + month);

        LocalDate localDate = LocalDate.of(year, month, dayOfMonth);
        // 1毫秒=1000000纳秒
        // unpack方法中设置的是毫秒, 所以这里一定要乘1000000, 否则会导致时间戳序列化前后不一致
        LocalTime localTime = LocalTime.of(hour, minute, second, millisecond * 1000000);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);


        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        return MResult.build(
                6,
                date
        );
    }

    @Override
    public byte[] unpack(Date object, String attachParams) {
        Date _date = object;
        if (object == null) {
            _date = new Date();
        }

        String yearBs = Integer.toBinaryString(DateUtil.year(_date));
        // 月份要加1
        String monthBs = Integer.toBinaryString(DateUtil.month(_date) + 1);

        String dayOfMonthBs = Integer.toBinaryString(DateUtil.dayOfMonth(_date));
        String hourBs = Integer.toBinaryString(DateUtil.hour(_date, true));
        String minuteBs = Integer.toBinaryString(DateUtil.minute(_date));
        String secondBs = Integer.toBinaryString(DateUtil.second(_date));
        String millisecondBs = Integer.toBinaryString(DateUtil.millisecond(_date));

        StringBuilder builder = new StringBuilder();

        // 年和月信息
        String yearAndMonthBs = ByteUtil.unsignedBinaryStringZeroFill(yearBs, 12, ByteUtil.BIT)
                + ByteUtil.unsignedBinaryStringZeroFill(monthBs, 4, ByteUtil.BIT);
        builder.append(ByteUtil.unsignedHexStringZeroFill(ByteUtil.binaryStringToHexString(yearAndMonthBs), 2, ByteUtil.B));

        // 日 时 分信息
        String dayOfMonthAndHourAndMinuteBs = ByteUtil.unsignedBinaryStringZeroFill(dayOfMonthBs, 5, ByteUtil.BIT)
                + ByteUtil.unsignedBinaryStringZeroFill(hourBs, 5, ByteUtil.BIT)
                + ByteUtil.unsignedBinaryStringZeroFill(minuteBs, 6, ByteUtil.BIT);

        builder.append(ByteUtil.unsignedHexStringZeroFill(ByteUtil.binaryStringToHexString(dayOfMonthAndHourAndMinuteBs), 2, ByteUtil.B));

        // 秒 毫秒信息
        String secondAndMillisecondBs = ByteUtil.unsignedBinaryStringZeroFill(secondBs, 6, ByteUtil.BIT)
                + ByteUtil.unsignedBinaryStringZeroFill(millisecondBs, 10, ByteUtil.BIT);
        builder.append(ByteUtil.unsignedHexStringZeroFill(ByteUtil.binaryStringToHexString(secondAndMillisecondBs), 2, ByteUtil.B));

        return ByteBufUtil.decodeHexDump(builder.toString());
    }
}
